package com.company.util;

import com.company.model.Task;
import com.company.service.TaskService;

import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ReminderManager {
        private final TaskService taskService;
        private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

        public ReminderManager(TaskService taskService) {
            this.taskService = taskService;
        }

        public void start() {
            // 每5分钟检查一次即将开始的任务
            scheduler.scheduleAtFixedRate(() -> {
                try {
                    List<Task> upcomingTasks = taskService.getUpcomingTasks(30); // 未来30分钟
                    if (!upcomingTasks.isEmpty()) {
                        System.out.println("\n--- 注意！ ---");
                        for (Task task : upcomingTasks) {
                            System.out.println("你有一项任务即将开始: " + task);
                        }
                        System.out.println("-----------------");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }, 0, 5, TimeUnit.MINUTES);
        }

        public void stop() {
            scheduler.shutdown();
        }
    }
